#ifndef BLOCK_H_INCLUDED
#define BLOCK_H_INCLUDED

typedef unsigned char byte;
#include <cassert>


struct _block {
	size_t size;
	_block **pp_prev_next;
	_block *next_free;
	int i;
	int j;

	size_t get_size() {
		return size & ~1;
	}

	size_t get_user_size() {
		return get_size() - sizeof(size_t);
	}

	bool is_prev_free() {
		return size & 1;
	}

	void is_prev_free(size_t x) {
		assert(0 == x || 1 == x);
		size &= ~1;
		size |= x;
	}

	void set_size(size_t sz) {
		sz &= ~1;
		size = sz | (size & 1);
	}

	_block* next() {
		return (_block *) ((byte *)this + get_size());
	}

	bool free() {
		return next()->is_prev_free();
	}

	byte* ptr() {
		return (byte *)this + sizeof(size_t);
	}

	_block *prev() {
		assert(is_prev_free());
		return *((_block **)this - 1);
	}

	void set_prev(_block *pbl) {
		assert(is_prev_free());
		*((_block **)this - 1) = pbl;
	}
};

#endif // BLOCK_H_INCLUDED
